﻿using ebMS3dotnet.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace ebMS3dotnet.Core
{
    public class ebMS3Binding:Binding
    {

        private Uri _mep;

        public Uri Mep
        {
            get { return _mep; }
            set { _mep = value; }
        }
        private Uri _mepBinding;

        public Uri MepBinding
        {
            get { return _mepBinding; }
            set { _mepBinding = value; }
        }

        private EnvelopeVersion _envelopeVersion;

        public EnvelopeVersion EnvelopeVersion
        {
            get { return _envelopeVersion; }
            set { _envelopeVersion = value; }
        }
        public ebMS3Binding(Uri mep, Uri mepBinding, EnvelopeVersion envelopeVersion):this(envelopeVersion)
        {
            _mep = mep;
            _mepBinding = mepBinding;
        }

        public ebMS3Binding(Uri mep, Uri mepBinding):this(mep, mepBinding, EnvelopeVersion.Soap12)
        {
        }

        public ebMS3Binding(EnvelopeVersion envelopeVersion)
        {
            _envelopeVersion = envelopeVersion;
        }
        public override BindingElementCollection CreateBindingElements()
        {
            HttpTransportBindingElement transportElement = new HttpTransportBindingElement();
            TextMessageEncodingBindingElement encodingElement = 
                new TextMessageEncodingBindingElement(CreateMessageVersion(), CreateEncoding());
            ebMS3BindingElement ebms3Element = new ebMS3BindingElement();

            return new BindingElementCollection(new BindingElement[]{
                ebms3Element,
                encodingElement,
                transportElement                
                });
        }

        internal virtual MessageVersion CreateMessageVersion()
        {
            return MessageVersion.CreateVersion(_envelopeVersion, AddressingVersion.None);
        }

        internal virtual Encoding CreateEncoding()
        {
            return Encoding.UTF8;
        }

        public override string Scheme
        {
            get { return "http"; }
        }
 
    }
}
